<?php
/**
 * Verwalten von statischen Routen
 *
 * @TODO, @NOTICE: Veraltete Klasse, bitte nicht mehr verwenden !!!
 *
 * @author		Joachim Ruf
 * @copyright	Copyright (c) 2003-2013 Loresoft Software (http://www.loresoft.de)
 * @license		http://yapary.loresoft.de/lizenzbedingungen     New BSD License
 * @TODO es koennte noch ein caching-problem bestehen
 * */


Class Yapary_Route_StaticRouteList
{
    /** @var gecachte Routendaten aus der Datenbank **/
    private $sqlRouteName = NULL;
    private $sqlRoute = NULL;
    private $dbTableName = 'yapary_static_route';

    private $oSql;


    /**
     * Klasse zur Verwaltung von statischen und dynamischen Routen.
     * {Route, Routenname, Controller, Action, URL}
     *
     * http://internetseite.de/?route=routeName
     * routeName => controller=abc, action=xyz
     * */
    function __construct (Yapary_Db_Sql_Connector $_oSql)
    {
		if (!$_oSql->database()->table($this->dbTableName)->exists()) {
            $e = new Exception();
            $arrTrace = $e->getTrace();
            $trace = '<b>'.$arrTrace[0]['class'].$arrTrace[0]['type'].$arrTrace[0]['function'].'</b> in <b>'.$arrTrace[0]['file'].'</b> on line <b>'.$arrTrace[0]['line'].'</b>';
            trigger_error('<pre></b>No database installed<br />'.$trace.'</pre>', E_USER_WARNING);
		}

		$this->oSql = $_oSql;
    }


    /**
     * Prueft ob es eine Route mit dem angegebenen Routennamen gibt
     * @param string $routeName		: Einmaliger Name der Route
     * @return bool
     * */
    public function exists ($routeName)
    {
        $oDbResult = $this->oSql->request('SELECT RouteName FROM '.$this->dbTableName.'
        					  WHERE RouteName=:routeName',
                             array('routeName' => $routeName));

        if ($oDbResult->getCount() == 0) {
            return false;
        }

        return true;
    }


    /**
     * erstellt eine neue Route
     * @param string $routeName		: Einmaliger Name der Route
     * @param string $url			: Url welche die Route repraesentiert inklusive Yapary_Placeholder {:varName}
     * @param string $controllerName: Controller auf welche die Route verweist
     * @param string $actionName	: Action auf welche die Route verweist
     * @param string $viewName 		: View welches die Daten ausgeben soll
     * @return Yapary_Route_StaticRoute
     * */
    public function add ($routeName, $url, $controllerName, $actionName, $viewName, array $arrVars = array())
    {
        $result = false;

        if (!$this->exists($routeName)) {
            $result =  $this->oSql->request('INSERT INTO '.$this->dbTableName.' (RouteName, Url, ControllerName, ActionName, ViewName, Vars)
                    					 VALUES(:routeName, :url, :controllerName, :actionName, :viewName, :vars)',
                                         array('routeName'      => $routeName,
                                               'url'            => $url,
                                               'controllerName' => $controllerName,
                                               'actionName'     => $actionName,
                                               'viewName'       => $viewName,
                                               'vars'           => serialize($arrVars)));

             if ($result === true) {
                 return new Yapary_Route_StaticRoute($this->oSql, $routeName);
             }
        }

        return false;
    }


    /**
     * Loescht ein bereits erstellte Route aus dem Routenplaner
     * @param string $routeName	: Name der zu loeschende Route
     * @return bool
     * */
    public function delete ($routeName)
    {
        if ($this->exists($routeName)) {
            return $this->oSql->request('DELETE FROM '.$this->dbTableName.'
            							WHERE RouteName=:routeName',
                                        array('routeName' => $routeName));
        }

        return false;
    }



    /**
     * Liefert anhand der statischen Url den Routennamen,
     * @param string $url 	: Url
     * @return Yapary_Route_StaticRoute
     * */
    public function getRouteByRouteName ($routeName) {
        $oDbResult = $this->oSql->request('SELECT * FROM '.$this->dbTableName.'
        					  WHERE RouteName=:routeName
        					  LIMIT 1',
                              array('routeName' => $routeName));

        if ($oDbResult->getCount() == 1) {
            $row = $oDbResult->getNext();
            return new Yapary_Route_StaticRoute($this->oSql, $row);
        }

        return NULL;
    }



    /**
     * Liefert anhand der statischen Url den Routennamen,
     * @param string $url 	: Url
     * @return Yapary_Route_StaticRoute
     * */
    public function getRoute ($url) {
    	return $this->get($url);
    }
    /**
     * Veraltete Klasse !!! bitte getRoute() verwenden
     * @NOTICE veraltete Klasse, bitte getRoute verwenden
     * */
    public function get ($url)
    {
        $oDbResult = $this->oSql->request('SELECT * FROM '.$this->dbTableName.'
        					  WHERE Url=:url
        					  LIMIT 1',
                              array('url' => $url));

        if ($oDbResult->getCount() == 1) {
            $row = $oDbResult->getNext();
            return new Yapary_Route_StaticRoute($this->oSql, $row);
        }

        return NULL;
    }


    /**
     * Liefert die anzahl aller vorhandenen statischen Routen
     * @return int
     * */
    public function getCount ()
    {
        $this->oSql->request('SELECT COUNT(*) AS Counter
        					FROM '.$this->dbTableName,
        					'routeCount');

        $row = $this->oSql->getData('routeCount');
        return (int) $row->Counter;
    }

}


?>